﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GCELibrary.Current;
 
using System.Linq.Dynamic;
using System.Linq.Expressions;
using ZF.BLL.Linq;
using ZF.BLL;


namespace ZF.BLL.Admin
{

    public partial class HouseList : ZF.BLL.Page.MMPage
    {
        public override ModuleType Module
        {
            get { return ModuleType.House; }
            protected set
            {
                base.Module = value;
            }
        }
        [PageArgument(Key = "dt", DefaultValue = -1)]
        public int DealType { get; set; }
        [PageArgument(Key = "days", DefaultValue = -1)]
        public int Days { get; set; }
        [PageArgument(Key = "jp", DefaultValue = 0)]
        public int IsBoutique { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
       
            if (!IsPostBack)
            {
                InitControls();
                View();
            }
        }
        public void InitControls()
        {
            this.ddlHouseType.DataSource = DictionaryConfig.HHouseType;
            this.ddlHouseType.DataBind();
        }
        public void View()
        {
            ListBind();
        }
        /// <summary>
        /// 列表绑定
        /// </summary>
        public void ListBind()
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {

                List<Expression<Func<Model.H_House, bool>>> filterList = new List<Expression<Func<Model.H_House, bool>>>();
                IQueryable<Model.H_House> selector = dc.H_House;
                if (this.CurrentUser.UserType != UserType.Admin)
                    filterList.Add(p => p.MemberId == this.CurrentUser.MemberId);
                if(DealType!=-1)
                    filterList.Add(p => p.DealType==this.DealType);
                //if (this.Days != -1)
                //    filterList.Add(p => p.AddTime >= DateTime.Now.AddDays(-this.Days));
                if(this.IsBoutique==1)
                    filterList.Add(p => p.IsBoutique);
                foreach (var filter in filterList)
                {
                    selector = selector.Where(filter);
                }
                var selWhere = selector.Select(p =>
                    new
                    {
                        House=p,
                    }).Where(this.gvList);
                var selPage = selWhere.OrderBy(this.gvList.SortExpression)
                        .Skip(gvList.PageIndex * gvList.PageSize).Take(gvList.PageSize);
                this.gvList.RowCount = selWhere.Count();
                string[] dtarr = { "", "整租", "合租", "二手", "一手", };
                var source = selPage.ToList().Select(p =>
                    {
                        var price="";
                        if(p.House.DealType == 1 || p.House.DealType == 2)
                            price=string.Format("{0} 元/月",p.House.Price);
                        else
                            price=string.Format("{0} 万元/套",p.House.Price);


                        return new
                        {
                            House = p.House,
              
                            City = SiteInfo.GetAreaOrDefault(p.House.AreaId1).Name,
                            Area = DictionaryConfig.HArea.DefaultIfEmpty(a => p.House.AreaId2.HasValue && a.AreaId == p.House.AreaId2.Value, new ZF.Model.Dic_Area()).Name,
                            HouseType =p.House.HouseType,
                            HouseModel=string.Format("{0}房{1}厅",p.House.HX_Fan,p.House.HX_Tin),
                            Price = price,
                            DT=dtarr[p.House.DealType]
                        };
                    }).ToList();
                this.gvList.DataSource = source;
                this.gvList.DataBind();

            }
        }
        /// <summary>
        /// 排序事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void gvListSorting(object sender, GridViewSortEventArgs e)
        {
            ListBind();
        }
        /// <summary>
        /// 换页事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void gvListPageChanging(object sender, GridViewPageEventArgs e)
        {
            this.gvList.PageIndex = e.NewPageIndex;
            ListBind();
        }
        public void Delete(params int[] idArr)
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {
                bool isSuccess = true;
                try
                {
                    dc.H_House.DeleteAllOnSubmit(
                       dc.H_House.Where(p => idArr.Contains(p.HouseId)));
                    dc.SubmitChanges();
                }
                catch (Exception ex)
                {
                    isSuccess = false;
                    throw SiteInfo.WarnFactory.CreateWarn(ex);
                }
                string msgKey = "房源";
                if (isSuccess)
                    WarnFactory.CreateWarn(string.Format("删除{0}成功", msgKey), WarnIcon.Success, WarnLink.Current).Alert();

            }
        }
        public void Delete(object sender, EventArgs e)
        {
            var bc = (IButtonControl)sender;
            if (bc.CommandArgument == "select")
            {
                var idArrTemp = this.gvList.GetCheckKeys();
                if (idArrTemp.Length == 0)
                    throw SiteInfo.WarnFactory.CreateWarn("未选择任何记录");
                var idArr = idArrTemp.Select(p => p.Convert<int>()).ToArray();
                Delete(idArr);
            }
            else
            {
                Delete(bc.CommandArgument.Convert<int>());
            }
        }
        public void Search(object sender, EventArgs e)
        {
            this.gvList.RemoveAllFilterExpression();
            if (this.tbTitle.Text.EmptyString() != null)
                this.gvList.AddFilterExpression("House.Title.Contains(@0)", this.tbTitle.Text);
            if (this.tbHouseNo.Text.EmptyString() != null)
                this.gvList.AddFilterExpression("House.HouseId=@0", this.tbHouseNo.Text.Convert<int>()-10000);
            if (this.ddlHouseType.SelectedValue != "-1")
                this.gvList.AddFilterExpression("House.HouseType=@0", this.ddlHouseType.SelectedValue);
            if (this.ddlDealType.SelectedValue != "0")
                this.gvList.AddFilterExpression("House.DealType=@0", this.ddlDealType.SelectedValue.Convert<int>());
            if (this.tbEndTime1.Text.EmptyString() != null)
                this.gvList.AddFilterExpression("House.AddTime<=@0", this.tbEndTime1.Text.Convert<DateTime>());
            if (this.tbEndTime2.Text.EmptyString() != null)
                this.gvList.AddFilterExpression("House.AddTime>=@0", this.tbEndTime2.Text.Convert<DateTime>());
            if (this.tbArea1.Text.EmptyString() != "0")
                this.gvList.AddFilterExpression("House.AreaId1=@0", this.tbArea1.Text.Convert<int>());
            if (this.tbArea2.Text.EmptyString() != "0")
                this.gvList.AddFilterExpression("House.AreaId2=@0", this.tbArea2.Text.Convert<int>());
            if (this.tbPrice1.Text.EmptyString() != null)
                this.gvList.AddFilterExpression("House.Price>=@0", this.tbPrice1.Text.Convert<int>());
            if (this.tbPrice2.Text.EmptyString() != null)
                this.gvList.AddFilterExpression("House.Price<=@0", this.tbPrice2.Text.Convert<int>());
            if (this.tbTin.Text.EmptyString() != null)
                this.gvList.AddFilterExpression("House.HX_Tin=@0", this.tbTin.Text.Convert<int>());
            if (this.tbFan.Text.EmptyString() != null)
                this.gvList.AddFilterExpression("House.HX_Fan=@0", this.tbFan.Text.Convert<int>());
            ListBind();
        }
        public void AllList(object sender, EventArgs e)
        {
            this.gvList.RemoveAllFilterExpression();
            ListBind();
        }

        public string GetStatus(int status)
        {
            switch (status)
            {
                case 1:
                    return "发布中";
                case 2:
                    return "停止发布";
                case 3:
                    return "完成交易";

            }
            return "";
        }
    }
}
